<?php

/* Created by: Adam Malevski
 * Date: 2011-12-07
 * Class works with feedback asks
 */

class Ask {
    var $link, $id, $data;
    
    function __construct($id = NULL, $foruid = NULL, $workid = NULL) {
        require_once 'db.class.php';
        include("mysql.config.php");
        $this->link = new DataBase($db_address, $db_username, $db_password, $db_db);
        if (isset ($foruid) && isset($workid)) {
            //$this->link->insert("asks", array($foruid,$workid), "`foruid`,`workid`");
            $this->link->query("INSERT INTO `asks`(`foruid`,`workid`) VALUES('$foruid','$workid')");
            $lid = mysql_insert_id();
            $this->link->select("asks", "*", "`id`='$lid'");
            $this->id  = $lid;
            $this->data = $this->link->getResult();
        }
        else {
            $this->link->select("asks", "*", "`id`='$id'");
            $this->id  = $id;
            $this->data = $this->link->getResult();
        }
    }
    
    /*
     * Function gets or sets finish flag
     * @return - returns true if feedback time ended and false if ask is ongoing
     * @rtype - boolean
     */
    function isFinished($flag = NULL) {
        if (empty($flag))
            return $this->data['finished'];
        else
            $this->link->query("UPDATE `asks` SET `finished` = '$flag' WHERE `id` = '$this->id'");
    }
    
    /*
     * Function gets category ID
     * @return - returns category id
     * @rtype - integer
     */
    function getId() {
        return $this->id;
    }
    
    /*
     * Function gets category name
     * @return - returns category name
     * @rtype - string
     */
    function getWorkId() {
        return $this->data['workid'];
    }
    
    /*
     * Function gets category name
     * @return - returns category name
     * @rtype - string
     */
    function getAskingUserId() {
        return $this->data['foruid'];
    }
    
    /*
     * Function gets category name
     * @return - returns category name
     * @rtype - string
     */
    function getPleasingUserId() {
        require_once '../work.class.php';
        $work = new Work($this->getWorkId());
        return $work->getUserId();
    }
    
    /*
     * Function sets category name
     * @return - 1 if set, 0 if fail
     * @rtype - boolean
     */
    function setWorkId($id) {
        $res = $this->link->query("UPDATE `asks` SET `workid` = '$id' WHERE `id` = '$this->id'");
        if ($res)
            return 1;
        else
            return 0;
    }
    
    function setPleasingUserId($id) {
        $res = $this->link->query("UPDATE `asks` SET `foruid` = '$id' WHERE `id` = '$this->id'");
        if ($res)
            return 1;
        else
            return 0;
    }
}

?>
